package com.stone.persistent.recyclerview.library;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;


public class ChildRecyclerViews extends BaseRecyclerView {
    private ParentRecyclerView parentRecyclerView;
    private int mTouchSlop;
    private float downX, downY;
    private int dragState;
    private static int DRAG_IDLE = 0;
    private static int DRAG_VERTICAL = 1;
    private static int DRAG_HORIZONTAL = 2;

    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        if (state == 0) {
            int velocityY = this.getVelocityY();
            if (velocityY < 0 && this.computeVerticalScrollOffset() == 0) {
                ParentRecyclerView var10000 = this.parentRecyclerView;
                if (var10000 != null) {
                    var10000.fling(0, velocityY);
                }
            }
        }

    }

    public boolean onInterceptTouchEvent( MotionEvent ev) {
        if (ev.getAction() == 0) {
            this.dragState = 0;
            this.downX = ev.getRawX();
            this.downY = ev.getRawY();
            this.stopFling();
        }
        return super.onInterceptTouchEvent(ev);
    }

    public boolean onTouchEvent( MotionEvent ev) {
        if (ev.getAction() == DRAG_HORIZONTAL && this.dragState == DRAG_IDLE) {
            float xDistance = Math.abs(ev.getRawX() - this.downX);
            float yDistance = Math.abs(ev.getRawY() - this.downY);
            if (xDistance > yDistance && xDistance > (float) this.mTouchSlop) {
                this.dragState = DRAG_HORIZONTAL;
            } else if (yDistance > xDistance && yDistance > (float) this.mTouchSlop) {
                this.dragState = DRAG_VERTICAL;
                this.getParent().requestDisallowInterceptTouchEvent(true);
            }
        }
        return super.onTouchEvent(ev);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.connectToParent();
    }

    private void connectToParent() {
        ViewPager viewPager = (ViewPager) null;
        ViewPager2 viewPager2 = (ViewPager2) null;
        View lastTraverseView = (View) this;
        ViewParent var10000 = this.getParent();
        if (var10000 == null) {
            try {
                throw new Exception("null cannot be cast to non-null type android.view.View");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            for (View parentView = (View) var10000; parentView != null; parentView = (View) var10000) {
                String parentClassName = parentView.getClass().getCanonicalName();
                // 使用ViewPager2，parentView的顺序如下:
                // ChildRecyclerView -> 若干View -> FrameLayout -> RecyclerViewImpl -> ViewPager2 -> 若干View -> ParentRecyclerView

                // 此时lastTraverseView是上方注释中的FrameLayout，算是"ViewPager2.child"，我们此处将ChildRecyclerView设置到FrameLayout的tag中
                // 这个tag会在ParentRecyclerView中用到
                if ("androidx.viewpager2.widget.ViewPager2.RecyclerViewImpl".equals(parentClassName)) {

                    lastTraverseView.setTag(R.id.tag_saved_child_recycler_view, this);

                } else if (parentView instanceof ViewPager) {

                    // 使用ViewPager，parentView顺序如下：
                    // ChildRecyclerView -> 若干View -> ViewPager -> 若干View -> ParentRecyclerView
                    // 此处将ChildRecyclerView保存到ViewPager最直接的子View中
                    if (lastTraverseView != this) {
                        // 这个tag会在ParentRecyclerView中用到
                        lastTraverseView.setTag(R.id.tag_saved_child_recycler_view, this);
                    }
                    // 碰到ViewPager，需要上报给ParentRecyclerView
                    viewPager = (ViewPager) parentView;

                } else if (parentView instanceof ViewPager2) {
                    viewPager2 = (ViewPager2) parentView;
                } else if (parentView instanceof ParentRecyclerView) {
                    ((ParentRecyclerView) parentView).setInnerViewPager(viewPager);
                    ((ParentRecyclerView) parentView).setInnerViewPager2(viewPager2);
                    ((ParentRecyclerView) parentView).setChildPagerContainer(lastTraverseView);
                    this.parentRecyclerView = (ParentRecyclerView) parentView;
                    return;
                }

                lastTraverseView = parentView;
                var10000 = parentView.getParent();
                if (var10000 == null) {
                    try {
                        throw new Exception("null cannot be cast to non-null type android.view.View");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public ChildRecyclerViews(@Nullable Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        this.mTouchSlop = configuration.getScaledTouchSlop();
    }


    public ChildRecyclerViews(Context var1, AttributeSet var2, int var3, int var4) {
        this(var1, var2, var3);
    }

    public ChildRecyclerViews(@Nullable Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0, 4);
    }

    public ChildRecyclerViews(@Nullable Context context) {
        this(context,null, 0, 6);
    }




}
